package com.ym.fc.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ym.fc.bean.User;
import com.ym.fc.common.Constants;
import com.ym.fc.dao.UserDao;
import com.ym.util.SerializeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * UserService
 *
 * @author yanm
 * @date 2021/1/7 15:31
 **/
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserService {
    private final UserDao userDao;

    private final RedisTemplate redisTemplate;

    public int add(User user){
        return userDao.insert(user);
    }

    public int updateById(User user){
        return userDao.updateById(user);
    }

    public int deleteById(Long id){
        return userDao.deleteById(id);
    }

    public List<User> select(User user){
        Wrapper wrapper =  new QueryWrapper(user);
        return userDao.selectList(wrapper);
    }

    public User select(Long id){
        return  userDao.selectById(id);
    }

    public User login(User user){
        /**
        * 检查缓存userId
        **/
        Boolean member = redisTemplate.opsForSet().isMember(Constants.REDIS_USERID, user.getUserId());
        if(member.equals(Boolean.TRUE)){
            String userJson = (String)redisTemplate.opsForValue().get(Constants.REDIS_USERS+":"+user.getUserId());
            return SerializeUtil.fromJson(userJson,User.class);
        }
        return null;
    }



}
